package org.lc.stk.model.question.attrs;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.annotations.Comment;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;

/**
 * 难度方案
 * 目前采用系统内置方案，不可自定义
 * 包括：
 * - 四级制：容易、一般、难、很难
 * - 五级制：容易、较易、一般、较难、很难
 * - 十分制：难度从1-10
 */
@Entity
@Table(name = "stk_difficulty_schema")
@Comment("难度方案")
public class DifficultySchema {

    public enum Type {
        FOUR_LEVEL("四级制", 4),
        FIVE_LEVEL("五级制", 5),
        TEN_LEVEL("十分制", 10);

        private final String label;
        private final int levelCount;

        Type(String label, int levelCount) {
            this.label = label;
            this.levelCount = levelCount;
        }

        public String getLabel() {
            return label;
        }

        public int getLevelCount() {
            return levelCount;
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 50, nullable = false, unique = true)
    @Comment("名称")
    private String name;

    @OneToMany(mappedBy = "difficultySchema", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("sortOrder ASC")
    @Comment("难度等级")
    private List<DifficultyLevel> ranks = new ArrayList<>();

    @Column(length = 500)
    @Comment("描述")
    private String description;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    @Comment("难度方案类型")
    private Type type;

    protected DifficultySchema() {
    }

    public DifficultySchema(String name, Type type, String description) {
        this.name = name;
        this.type = type;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    protected void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Type getType() {
        return type;
    }

    protected void setType(Type type) {
        this.type = type;
    }

    public List<DifficultyLevel> getRanks() {
        return ranks;
    }

    protected void setRanks(List<DifficultyLevel> ranks) {
        this.ranks = ranks;
    }

    /**
     * 添加难度等级
     * @param rank 难度等级
     */
    public void addRank(DifficultyLevel rank) {
        ranks.add(rank);
        rank.setDifficultySchema(this);
    }

    /**
     * 移除难度等级
     * @param rank 难度等级
     */
    public void removeRank(DifficultyLevel rank) {
        ranks.remove(rank);
        rank.setDifficultySchema(null);
    }

    /**
     * 清空所有难度等级
     */
    public void clearRanks() {
        ranks.forEach(rank -> rank.setDifficultySchema(null));
        ranks.clear();
    }
}
